package com.wcgksoft.controller.system;

import com.wcgksoft.common.util.FileHandleUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.system.OnTrial;
import com.wcgksoft.service.system.OnTrialService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @author 阿良
 * @version 1.0.0
 * @ClassName OnTrialController.java
 * @Description 试用信息提交查询3删除修改
 * @createTime 2020年03月30日 08:56:00
 */
@Controller
@RequestMapping("ontrial")
public class OnTrialController {
  @Autowired
  private OnTrialService onTrialService;




  /**
   * @throws
   * @title TODO
   * @description 试用列表表
   * @author 阿良
   * @param: page
   * @param: limit
   * @updateTime 2020/3/6 15:23
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "list")
  public Layui findAll(
          @RequestParam(value = "page", required = false) Integer page,
          @RequestParam(value = "limit", required = false) Integer limit) {

    DetachedCriteria criteria = DetachedCriteria.forClass(OnTrial.class);
    criteria.addOrder(Order.desc("add_time"));
    List<OnTrial> list = onTrialService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", onTrialService.getTotalCount(), list);

  }


  /**
   * @title TODO
   * @description 添加修改企业
   * @author 阿良
   * @param: request
   * @param: data
   * @param: meun
   * @updateTime 2020/3/11 10:31
   * @return: com.wcgksoft.common.util.Layui
   * @throws
   */
  @ResponseBody
  @RequestMapping(value = "update")
  public Layui update(HttpServletRequest request,
                      @RequestParam(value = "data", required = false) String data,
                      @RequestParam(value = "file",required = false) MultipartFile[] file) throws Exception {
    JSONObject param = JSONObject.fromObject(data);
    if (param == null) {
      return Layui.data(1, "请求格式有误", 0, null);
    } else {
      OnTrial onTrial = (OnTrial) JSONObject.toBean(JSONObject.fromObject(data), OnTrial.class);
      for (MultipartFile mf : file) {
        if (!mf.isEmpty()) {
          String filename = FileHandleUtil.Upset(mf);
          onTrial.setImgurl("upload/picture/" + FileHandleUtil.upload(mf.getInputStream(), "upload/picture/", filename, request));
        }
      }
      onTrial.setAdd_time(new Date());
      onTrialService.save(onTrial);
      return Layui.data(0, "保存成功", 1, Layui.addList(onTrial));
    }
  }
  /**
   * 删除
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "delete")
  @Transactional
  public Layui delete(@RequestParam(value = "id") Integer id) {
    OnTrial byId = onTrialService.findById(id);
    onTrialService.deleteCass(byId);

    return Layui.data(1, "删除成功", 1, null);
  }








}
